// 权限控制工具

// 权限常量
export const PERMISSIONS = {
  ALL: 'all',
  MANAGE_USER: 'manage_user',
  APPROVE: 'approve',
  MATCHMAKING: 'matchmaking',
  CONTENT_EDIT: 'content_edit'
}

// 模块权限映射
export const MODULE_PERMISSIONS = {
  '/student-management': PERMISSIONS.MANAGE_USER,
  '/company-management': PERMISSIONS.MANAGE_USER,
  '/student-review': PERMISSIONS.APPROVE,
  '/company-review': PERMISSIONS.APPROVE,
  '/demand-review': PERMISSIONS.APPROVE,
  '/request': PERMISSIONS.MATCHMAKING,
  '/student-request': PERMISSIONS.MATCHMAKING,
  '/announcement': PERMISSIONS.CONTENT_EDIT,
  '/banner-management': PERMISSIONS.CONTENT_EDIT,
  '/admin-management': PERMISSIONS.MANAGE_USER,
  '/admin-roles': PERMISSIONS.MANAGE_USER
}

// 获取用户权限
export function getUserPermissions() {
  const permissions = localStorage.getItem('permissions')
  if (permissions) {
    try {
      return JSON.parse(permissions)
    } catch (e) {
      return []
    }
  }
  return []
}

// 检查是否有指定权限
export function hasPermission(permission) {
  const permissions = getUserPermissions()
  return permissions.includes(PERMISSIONS.ALL) || permissions.includes(permission)
}

// 检查是否有模块访问权限
export function hasModulePermission(modulePath) {
  const requiredPermission = MODULE_PERMISSIONS[modulePath]
  if (!requiredPermission) {
    return true // 没有权限要求的模块默认允许访问
  }
  return hasPermission(requiredPermission)
}

// 检查是否可以修改账号密码
export function canModifyAccount() {
  const permissions = getUserPermissions()
  return permissions.includes(PERMISSIONS.ALL)
}

// 获取用户可见的菜单项
export function getVisibleMenuItems() {
  const permissions = getUserPermissions()
  const hasAllPermissions = permissions.includes(PERMISSIONS.ALL)
  
  const menuItems = [
    {
      path: '/dashboard',
      title: '数据概览',
      icon: 'DataBoard',
      visible: true
    },
    {
      title: '审核管理',
      icon: 'Document',
      children: [
        { path: '/student-review', title: '留学生审核', permission: PERMISSIONS.APPROVE },
        { path: '/company-review', title: '企业认证审核', permission: PERMISSIONS.APPROVE },
        { path: '/demand-review', title: '需求内容审核', permission: PERMISSIONS.APPROVE }
      ]
    },
    {
      title: '请求管理',
      icon: 'Message',
      children: [
        { path: '/request', title: '企业联系请求', permission: PERMISSIONS.MATCHMAKING },
        { path: '/student-request', title: '学生对接请求', permission: PERMISSIONS.MATCHMAKING }
      ]
    },
    {
      title: '用户管理',
      icon: 'User',
      children: [
        { path: '/student-management', title: '学生管理', permission: PERMISSIONS.MANAGE_USER },
        { path: '/company-management', title: '企业管理', permission: PERMISSIONS.MANAGE_USER }
      ]
    },
    {
      path: '/announcement',
      title: '公告管理',
      icon: 'Bell',
      permission: PERMISSIONS.CONTENT_EDIT
    },
    {
      path: '/banner-management',
      title: '轮播图管理',
      icon: 'Picture',
      permission: PERMISSIONS.CONTENT_EDIT
    },
    {
      title: '管理员模块',
      icon: 'Setting',
      children: [
        { path: '/admin-management', title: '管理员管理', permission: PERMISSIONS.MANAGE_USER },
        { path: '/admin-roles', title: '角色管理', permission: PERMISSIONS.MANAGE_USER }
      ]
    },
    {
      path: '/operation-logs',
      title: '操作日志',
      icon: 'Document',
      visible: true
    },
    {
      path: '/user-agreement',
      title: '用户协议和隐私政策',
      icon: 'Document',
      visible: true
    }
  ]
  
  return menuItems.filter(item => {
    if (hasAllPermissions) return true
    if (item.visible) return true
    if (item.permission) return hasPermission(item.permission)
    if (item.children) {
      item.children = item.children.filter(child => 
        hasPermission(child.permission)
      )
      return item.children.length > 0
    }
    return false
  })
} 